package game.graphics.effects;

import java.util.LinkedList;
import java.util.List;

/**
 * Cares about all temporary graphic effects in a game.
 * It's a singleton, because because it should be accesible
 * from all parts of application.
 * 
 * @author Tomas Zima
 */
public class GraphicEffectsManager
{
	/**
	 * Constructor.
	 */
	private GraphicEffectsManager()
	{
		//
	}
	
	/**
	 * Returns instance of GraphicEffectsManager
	 * (there's only one in whole application).
	 * 
	 * @return
	 * 	Instance of GraphicEffectsManager.
	 */
	public static GraphicEffectsManager getInstance()
	{
		if (instance == null)
		{
			instance = new GraphicEffectsManager();
		}
		
		return instance;
	}
	
	/**
	 * Updates all graphic effects in the game.
	 * 
	 * @param deltaTime
	 */
	public void update(long deltaTime)
	{
		for (int i = 0; i < graphicEffects.size(); i++)
		{
			if (this.graphicEffects.get(i).isDone())
			{
				this.graphicEffects.remove(i++);
				break;
			}
			else
			{
				this.graphicEffects.get(i).update(deltaTime);
			}
		}
	}
	
	/**
	 * Redraws all graphic effects in the game.
	 * 
	 * @param cameraX
	 * @param cameraY
	 */
	public void redraw(int cameraX, int cameraY)
	{
		for (int i = 0; i < graphicEffects.size(); i++)
		{
			this.graphicEffects.get(i).redraw(cameraX, cameraY);
		}
	}
	
	/**
	 * Adds a new graphic effect.
	 * 
	 * @param graphicEffect
	 */
	public void registerNewEffect(IGraphicEffect graphicEffect)
	{
		this.graphicEffects.add(graphicEffect);
	}
	
	// List of all graphic effects
	private List<IGraphicEffect> graphicEffects = new LinkedList<IGraphicEffect>();
	
	// Pointer to instance of GraphicEffectsManager
	private static GraphicEffectsManager instance = null;
}
